package com.yoosora.plugins;

import cn.hutool.core.util.EnumUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.mikuac.shiro.annotation.MessageHandler;
import com.mikuac.shiro.common.utils.MsgUtils;
import com.mikuac.shiro.common.utils.RegexUtils;
import com.mikuac.shiro.core.Bot;
import com.mikuac.shiro.core.BotPlugin;
import com.mikuac.shiro.dto.event.message.WholeMessageEvent;
import com.yoosora.common.consts.ConstsS;
import com.yoosora.model.AoeIVDTO;
import com.yoosora.utils.AoeIVEnum;
import com.yoosora.utils.ApiConst;
import com.yoosora.utils.Collections;
import com.yoosora.utils.RegexConst;
import com.yoosora.utils.RequestUtils;
import com.yoosora.utils.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Component
public class AoeIVPlugin extends BotPlugin {
	static StringBuilder sb = new StringBuilder();
	static LinkedHashMap<String, AoeIVEnum> enumMap = EnumUtil.getEnumMap(AoeIVEnum.class);
	static Set<String> enumKey = enumMap.keySet();
	private final static String FORMATERROR = "请按照 aoeiv [qm1v1|qm2v2|qm3v3|qm4v4] [玩家名] 的格式发送指令";

	@MessageHandler(cmd = RegexConst.AOE_IV)
	public void aoeIVMsg(@NotNull Bot bot, @NotNull WholeMessageEvent event) {
		String msg = event.getMessage();
		long groupId = event.getGroupId();
		String leaderboard = RegexUtils.regexGroup(RegexConst.AOE_IV, msg, 1);
		String name = RegexUtils.regexGroup(RegexConst.AOE_IV, msg, 2);
		if (null != name && null != leaderboard && Strings.isNotEmpty(leaderboard) && Strings.isNotEmpty(name)) {
			leaderboard = leaderboard.toUpperCase();
			if (enumKey.contains(leaderboard)) {
				Map<String, String> param = Maps.newHashMap();
				AoeIVEnum aoeIVEnum = AoeIVEnum.valueOf(leaderboard);
				param.put("game", "aoe4");
				param.put("leaderboard_id", aoeIVEnum.getCode().toString());
				param.put("search", name);
				String result = RequestUtils.builder().get().build(ApiConst.AOE_IV, false, null, param);
				if(Strings.isEmpty(result)){
					sb.append("aoeIV接口异常，获取数据失败!");
				}else{
					AoeIVDTO aoeIVDTO = JSON.parseObject(result, AoeIVDTO.class);
					List<Map<String, String>> leaderboardList = aoeIVDTO.getLeaderboard();
					if (null != leaderboardList && Collections.isNotEmpty(leaderboardList)) {
						Map<String, String> leaderboardMap = leaderboardList.get(0);
						sb.append("帝国时代4 ").append(name).append("玩家").append(ConstsS.N)
								.append(leaderboard).append("匹配模式:")
								.append("排名：").append(leaderboardMap.get("rank")).append(ConstsS.N)
								.append("分数：").append(leaderboardMap.get("rating")).append(ConstsS.N)
								.append("总场数：").append(leaderboardMap.get("games")).append(ConstsS.N)
								.append("胜场：").append(leaderboardMap.get("wins")).append(ConstsS.N)
								.append("败场：").append(leaderboardMap.get("losses")).append(ConstsS.N);
					} else {
						sb.append("帝国时代4 ").append(name).append("玩家 暂无")
								.append(leaderboard).append("匹配模式的数据");
					}
				}
			} else {
				sb.append(FORMATERROR);
			}
		} else {
			sb.append(FORMATERROR);
		}
		bot.sendMsg(event, MsgUtils.builder().text(sb.toString()).build(), false);
		sb.setLength(0);
	}

	/*@Override
	public int onGroupMessage(@NotNull Bot bot, @NotNull GroupMessageEvent event) {
		String msg = event.getMessage();
		if (msg.matches(RegexConst.AOE_IV)) {
			long groupId = event.getGroupId();
			String leaderboard = RegexUtils.regexGroup(RegexConst.AOE_IV, msg, 1);
			String name = RegexUtils.regexGroup(RegexConst.AOE_IV, msg, 2);
			if (null != name && null != leaderboard && Strings.isNotEmpty(leaderboard) && Strings.isNotEmpty(name)) {
				leaderboard = leaderboard.toUpperCase();
				if (enumKey.contains(leaderboard)) {
					Map<String, String> param = Maps.newHashMap();
					AoeIVEnum aoeIVEnum = AoeIVEnum.valueOf(leaderboard);
					param.put("game", "aoe4");
					param.put("leaderboard_id", aoeIVEnum.getCode().toString());
					param.put("search", name);
					String result = RequestUtils.builder().get().build(ApiConst.AOE_IV, false, null, param);
					AoeIVDTO aoeIVDTO = JSON.parseObject(result, AoeIVDTO.class);
					List<Map<String, String>> leaderboardList = aoeIVDTO.getLeaderboard();
					if (null != leaderboardList && Collections.isNotEmpty(leaderboardList)) {
						Map<String, String> leaderboardMap = leaderboardList.get(0);
						sb.append("帝国时代4 ").append(name).append("玩家").append(ConstsS.RN)
								.append(leaderboard).append("匹配模式:")
								.append("排名：").append(leaderboardMap.get("rank")).append(ConstsS.RN)
								.append("分数：").append(leaderboardMap.get("rating")).append(ConstsS.RN)
								.append("总场数：").append(leaderboardMap.get("games")).append(ConstsS.RN)
								.append("胜场：").append(leaderboardMap.get("wins")).append(ConstsS.RN)
								.append("败场：").append(leaderboardMap.get("losses")).append(ConstsS.RN);
					} else {
						sb.append("帝国时代4 ").append(name).append("玩家 暂无")
								.append(leaderboard).append("匹配模式的数据");
					}
				} else {
					sb.append(FORMATERROR);
				}
			} else {
				sb.append(FORMATERROR);
			}
			bot.sendGroupMsg(groupId, MsgUtils.builder().text(sb.toString()).build(), false);
		}
		sb.setLength(0);
		return MESSAGE_IGNORE;
	}*/
}
